package com.sunny.example.string;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class HJ33_整数与IP地址间的转换 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String ip1 = scanner.nextLine();
            String ip2 = scanner.nextLine();
            //ip1:127.0.0.1
            //ip2:12345678
            if (!ip1.contains(".") || !ip2.matches("[0-9]+")) {
                continue;
            }
            String[] ip1Strs = ip1.split("\\.");
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : ip1Strs) {
                int num = Integer.parseInt(str);
                String num2 = Integer.toBinaryString(num);
                while (num2.length() < 8) {
                    num2 = "0" + num2;
                }
//                stringBuffer.append(int2Binary(num));
                stringBuffer.append(num2);
            }

            //十进制长整数转为ip地址
            long result = binary2Long(stringBuffer.toString());

            String result2 = num2ip(ip2);
            System.out.println(Long.parseLong(stringBuffer.toString(), 2));
            System.out.println(result2);

        }
    }

    /**
     * 数字转Ip
     *
     * @param ip2
     * @return
     */
    private static String num2ip(String ip2) {
        long num = Long.parseLong(ip2);
        //转二进制字符串
        String num2 = Long.toBinaryString(num);
        //然后再8位8位的拆分
        while (num2.length() < 32) {
            num2 = "0" + num2;//拆分前补0，补足32位
        }
        String[] result = new String[4];
        for (int i = 0; i < result.length; i++) {
            String str = num2.substring(8 * i, 8 * i + 8);
            str = Integer.toString(Integer.parseInt(str, 2));//转10进制
            result[i] = str;
        }

        return String.join(".", result);
    }

    /**
     * 字符串二进制转10进制长整数
     *
     * @param toString
     * @return
     */
    private static long binary2Long(String toString) {
        int result = 0;
        char[] chars = toString.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            int v = chars[i];
            result += v * Math.pow(2, toString.length() - i - 1);
        }
        return result;
    }

    /**
     * 十进制转二进制
     *
     * @param num
     */
    private static String int2Binary(int num) {
        List<Integer> result = new ArrayList<>();
        result.add(0, num % 2);
        int temp = num / 2;
        while (temp > 1) {
            result.add(0, temp % 2);
            temp = temp / 2;
        }
        result.add(0, 1);
        String str = String.valueOf(result.stream().toArray());
        StringBuffer stringBuffer = new StringBuffer();
        if (str.length() < 8) {
            for (int i = 0; i < 8 - str.length(); i++) {
                stringBuffer.append("0");
            }
        }
        stringBuffer.append(str);
        return stringBuffer.toString();

    }
}
